<?php

class GroupController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
		$this->beforeFilter('auth', array('except' => array('getAPI', 'getUsersAPI', 'storeAPI', 'updateUsersAPI', 'updateAPI', 'destroyAPI')));
	}
	
	/**
	 * Display a listing of groups.
	 *
	 * @return Response
	 */
	public function index()
	{
		// get all the matches
		$groups = Group::all();

		// load the view and pass the matches
		return View::make('groups.index')
			->with('groups', $groups);
	}


	/**
	 * Show the form for creating a new group.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created group in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}


	/**
	 * Display the specified group.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		// get the field
		$group = Group::find($id);

		// show the edit form and pass the field
		return View::make('groups.show')
			->with('group', $group);
	}


	/**
	 * Show the form for editing the specified group.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified group in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}


	/**
	 * Remove the specified group from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
	
	/*
	 * API Methods
	*/
	/**
	 * Display a listing of groups in JSON format.
	 *
	 * @return JSON Response
	 */
	public function getAPI($id = null)
	{
		if (is_null($id))
			return Response::json(Group::all());
		else
		{
			$group = Group::find($id);
			if(is_null($group)){
				return Response::json('Group not found', 404);
			} else {
				return Response::json($group);
			}
		}
	}
	
	/**
	 * Display a listing of users in the specified group in JSON format.
	 *
	 * @param groupId
	 * @return JSON Response
	 */
	public function getUsersAPI($groupId)
	{
		$group = Group::find($groupId);
		if(is_null($group)){
			return Response::json('Group not found', 404);
		} else {
			$group->admin;
			$group->users;
			return Response::json($group);
		}
	}
	
	/**
	 * Store a newly created group in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function storeAPI()
	{
		$newGroup = Input::json();
	
		$group = new Group();
		$group->name = $newGroup->get('name');
		$group->image = $newGroup->get('image');
		$group->admin_userId = $newGroup->get('admin_userId');
		$group->save();
	
		return Response::json($group);
	}
	
	/**
	 * Update the specified group in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function updateAPI()
	{
		$updateGroup = Input::json();
	
		$group = Group::find($updateGroup->get('id'));
		if(is_null($group)){
			return Response::json('Group not found', 404);
		}
		$group->name = $updateGroup->get('name');
		$group->image = $updateGroup->get('image');
		$group->admin_userId = $updateGroup->get('admin_userId');
		$group->save();
	
		return Response::json($group);
	}
	
	/**
	 * Update specified users into specified group by JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function updateUsersAPI()
	{
		$data = Input::json();
	
		// store relation to User
		$groupId = $data->get('groupId');
		$group = Group::find($groupId);
		$userIds = $data->get('userIds');
		if(is_array($userIds))
		{
			$group->users()->sync($userIds);
		}
	
		$group->admin;
		$group->users;
		return Response::json($group);
	}
	
	/**
	 * Remove the specified group from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroyAPI($id)
	{
		$group = Group::find($id);
	
		if(is_null($group))
		{
			return Response::json('Group not found', 404);
		}
		$deletedGroup = $group;
		$group->delete();
		return Response::json($deletedGroup);
	}


}
